/**
 * 
 */
package com.angel.httpunit;

import org.junit.Test;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebImage;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * @author William
 * 
 */
public class HttpUnitTestCase {

	@Test
	public void testHttpUnit(){
		try {
			// Create a conversation for maintaining state
			WebConversation wc = new WebConversation();

			// Obtain the home page on Google
			WebRequest request = new GetMethodWebRequest( "http://www.google.com.ar");
			WebResponse response = wc.getResponse( request );

			// Let's see how large Google's home page is
			int size = response.getContentLength();

			// Print out the page size
			System.out.println( "The Google home page is " + size + " Kb.");

			// Extract all the images
			WebImage[] imagesMainPage = response.getImages();
			int imgSizes = 0;

			// Print out the number of images in the home page
			System.out.println( "The Google home page contains " + imagesMainPage.length + " images" );

			for (int i = 0; i < imagesMainPage.length; i++) {
				WebRequest imgRequest = imagesMainPage[i].getRequest();
				WebResponse imgResponse = wc.getResponse( imgRequest );
				int imgSize = imgResponse.getContentLength();
				System.out.println(" Image " + (i+1) + " is : " + imgSize + " Kb.");
				imgSizes += imgSize;
			}

			// Total payload
			size += imgSizes;

			System.out.println("Total bandwidth for each home page visit " + size + " Kb.");

			// The daily bandwidth for Google at 200 million visits a day
			double bwInMillionsKB = size * 200;

			// 1 GB to KB
			int GBToKB = 1024*1024;

			// Total bandwidth in millions of GB
			double bwInMillionsGB = bwInMillionsKB/GBToKB;

			// Total bandwidth in GB
			double totalbwInGB = bwInMillionsGB * 1000000;

			// Print out the daily bandwidth for Google's home page
			System.out.println( "Total daily bandwidth at 200 million visits : " + totalbwInGB + " GB" );

		} catch (Exception e) {
			System.err.println( "Exception: " + e );
		}
	}
}
